package randomQueensSolution;

import java.awt.Point;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class RandomQueensMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		FileWriter writer = null;
		try {
			writer = new FileWriter(new File("RandomQueensSolution.csv"));
			
			writer.write(",Fintness\n");
			
			for(int i=0 ; i < 4000000 ; ++i)
			{
				List<Point> queensLocatoins;
				Set<Point> lacationsSet = new HashSet<Point>();
				Random random = new Random();
				for(int j = 0 ; j < 8 ; j++)
				{
					boolean valid;
					do
					{
						valid = lacationsSet.add(new Point(random.nextInt(8),random.nextInt(8)));
					}
					while(!valid);
				}
				queensLocatoins  = new LinkedList<Point>(lacationsSet);	
				double fit = getFitness(queensLocatoins);
//				System.out.println(fit); //1,233,957   2,439,006
//				writer.write(i+","+fit+"\n");
				if(fit == 0)
				{
					System.out.println("END "+i);
					break;
				}
			}
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	

	 public static double getFitness(List<Point> queens) {
		 
         double fitness = 0;
         int x1,x2,y1,y2;
         for(int i = 0 ; i < 8 ; ++i)
         {
                 for(int j = i+1 ; j < 8 ; ++j)
                 {
                         x1 = ((Point) queens.get(i)).x;
                         y1 = ((Point) queens.get(i)).y;
                         x2 = ((Point) queens.get(j)).x;
                         y2 = ((Point) queens.get(j)).y;                      
                         if( x1 == x2 || y1 == y2 || (x2-x1) == (y2-y1))
                         {
                                 fitness++;
                         }
                 }
         }
         return fitness;
//         return (1.0 / (fitness + 1))*100;
 }

}
